import tkinter
from tkinter import *
import os
from tkinter import filedialog

import matplotlib
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy.random.common
import numpy.random.bounded_integers
import numpy.random.entropy
LOGO_PATH = "F:/fish.ico"
window = tkinter.Tk()
window.title("数据转化为图片")
window.iconbitmap(LOGO_PATH)
window.geometry("700x180")
# ----------------------------------
filename = StringVar()
datastride = StringVar(value = 3333)
datalength = StringVar(value = 10000)
savepath = StringVar()
FileKey = StringVar(value = "Whole")

lblDataPath = tkinter.Label(window, text="数据路径").grid(row=0,column = 0)
lblSavePath = tkinter.Label(window, text="存图路径").grid(row=1,column = 0)
lblDataLength = tkinter.Label(window, text="数据长度").grid(row=2,column = 0)
lblDataStride = tkinter.Label(window, text="滑动长度").grid(row=3,column = 0)
lblFileKey = tkinter.Label(window, text="文件关键字").grid(row=5,column = 0)

txtDataPath = tkinter.Entry(window,width=60,textvariable=filename).grid(row=0,column = 1)
txtSavePath = tkinter.Entry(window,width=60,textvariable = savepath).grid(row=1,column = 1)
txtDataLength = tkinter.Entry(window,width=10,textvariable = datalength).grid(row=2,column = 1,sticky='w')
txtDataStride = tkinter.Entry(window,width=10,textvariable = datastride).grid(row=3,column = 1,sticky='w')
txtFileKey = tkinter.Entry(window,width=10,textvariable = FileKey).grid(row=5,column = 1,sticky='w')

IsShowCoordinate = IntVar()
IsSavePic = IntVar()
ckbCoordinate = Checkbutton(window, text = "显示坐标", variable = IsShowCoordinate).grid(row=3,column = 3)
ckbSavePic= Checkbutton(window, text = "保存图片", variable = IsSavePic).grid(row=4,column = 3)


pic_index = IntVar()
IsDrawOnce = IntVar()


def OpenFile():
    IsDrawOnce.set(0)
    file = filedialog.askopenfilename()
    if(file):
      filename.set(file)

def OpenDirectory(IsOpenOrSave):
    directory = filedialog.askdirectory()
    if(directory):
        if(IsOpenOrSave == 0):
            filename.set(directory)
        else:
            savepath.set(directory)

def split_data(filepath):
    data = pd.read_csv(filepath, sep=' ')
    size = data.shape[0]
    len = int(datalength.get())
    stride = int(datastride.get())
    start = 0
    end = start + len

    while(end<size):
        plot_data(data,start,end)
        start += stride
        end = start + len
    end = size
    start = end-len
    plot_data(data,start,end)

def plot_data(data,start,end):
    i = pic_index.get()
    i += 1
    pic_index.set(i)
    #print(i)
    #print(start)
    #print(end)
    data_cut = data[start:end]
    ax = plt.gca()
    ax.yaxis.set_major_locator(MultipleLocator(0.01))
    plt.plot(data_cut, color = 'black')
    if (IsShowCoordinate.get() == 0):
        plt.axis('off')
    if (IsSavePic.get() == 1):
        plt.savefig(savepath.get() + "/{}.png".format(i))
        plt.close()
    else:
        plt.show()
        plt.close()



def listSameFile(dir):
    str = FileKey.get()
    for x in os.listdir(dir):
        path = os.path.join(dir, x)
        if os.path.isfile(path) and str in os.path.splitext(x)[0]:
            print(path)
            split_data(path)
        if os.path.isdir(path):
            listSameFile(path)

def drawing():
    listSameFile(filename.get())

def draw_one_pic():
    split_data(filename.get())



def btn_OpenFile_click():
    IsDrawOnce.set(0)
    OpenFile()

def btn_OpenDirectory_click():
    IsDrawOnce.set(1)
    OpenDirectory(0)

def btn_SaveDirectory_click():
    OpenDirectory(1)

def btn_draw_click():
    pic_index.set(0)
    if(IsDrawOnce.get()==0):
        draw_one_pic()
    else:
        drawing()

Button(window, text="打开文件", command=btn_OpenFile_click).grid(row=0,column = 4)
Button(window, text="打开文件夹", command=btn_OpenDirectory_click).grid(row=0,column = 5)
Button(window, text="选择路径", command=btn_SaveDirectory_click).grid(row=1,column = 4)
Button(window, text="绘制", command=btn_draw_click,width = 15,height = 2).grid(row=3,column = 4,rowspan = 2,columnspan = 2)

window.mainloop()



